﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MyApplyRootMotion : MonoBehaviour
{
    public Transform m_flagObject;  // 用来测试位置的一个对象  

    //-- Root Motion 控制变量  
    public Transform m_rootBone;
    public Vector3 m_lastRootPos;
    public Vector3 m_rootMotion;
    public int m_lastAnimTime;

    public bool m_IsWeapon;
    public List<string> m_AnimNameList;
    //public string m_AnimName;

    void Start()
    {
        //-- 从SkinnedMeshRenderer中读取Root Bone  
        //SkinnedMeshRenderer skinMesh = this.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();
        //m_rootBone = skinMesh.rootBone;

        //-- 变量初始化  
        m_rootMotion = Vector3.zero;
        m_lastRootPos = m_rootBone.localPosition;
        m_lastAnimTime = 0;
    }

    void Update()
    {
        //-- Apply Root Motion  
        Vector3 nextPos = this.transform.position + m_rootMotion;
        this.transform.position = nextPos;

        //-- 测试代码：更新测试物体的位置  
        //Vector3 flagPos = m_flagObject.position;
        //flagPos.x = nextPos.x;
        //flagPos.z = nextPos.z;
        //m_flagObject.position = flagPos;

        //-- 测试代码：更新摄像机  
        //Camera.main.transform.LookAt(this.transform);
    }

    void LateUpdate()
    {
        Animation anim = this.gameObject.GetComponent<Animation>();
        //AnimationState animState = anim[m_AnimName];

        foreach (string animName in m_AnimNameList)
        {
            AnimationState animState = anim[animName];
            if (anim.IsPlaying(animName))
            {
                if ((int)animState.normalizedTime > m_lastAnimTime)
                {
                    //-- 动画循环处理  
                    m_lastRootPos = m_rootBone.localPosition;
                    m_rootMotion = Vector3.zero;
                }
                else
                {
                    //-- 计算当前帧的Root Motion  
                    Vector3 rootPos = m_rootBone.localPosition;
                    Vector3 tempRootPos = rootPos - m_lastRootPos;

                    m_lastRootPos = rootPos;
                    rootPos.x = 0;
                    rootPos.z = 0;

                    //Quaternion作用于Vector3的右乘操作（*）返回一个将向量做旋转操作后的向量.
                    //这里计算出根据模型朝向旋转操作后的root位移向量，
                    //不计算如果直接m_rootMotion =tempRootPos的话模型将只会在Z轴正方向有位移
                    //m_rootMotion = Quaternion.Euler(0, this.gameObject.transform.eulerAngles.y, 0) * tempRootPos;

                    m_rootMotion.y = 0;

                    m_rootBone.localPosition = rootPos;
                }
                m_lastAnimTime = (int)animState.normalizedTime;

                return;
            }
        }

        //武器的节点的localPos设为zero
        //if(gameObject.transform.parent.gameObject.CompareTag("Player"))
        //{
        //gameObject.transform.localPosition = Vector3.zero;
        //}
        if (m_IsWeapon == true)
        {
            gameObject.transform.localPosition = Vector3.zero;
        }


        ////    //-- 动画循环处理  
        //m_lastRootPos = m_rootBone.localPosition;
        //m_rootMotion = Vector3.zero;

        //m_lastAnimTime = (int)animState.normalizedTime;

        //if ((int)animState.normalizedTime > m_lastAnimTime)
        //{
        //    //-- 动画循环处理  
        //    m_lastRootPos = m_rootBone.localPosition;
        //    m_rootMotion = Vector3.zero;
        //}
        //else
        //{
        //    //-- 计算当前帧的Root Motion  
        //    Vector3 rootPos = m_rootBone.localPosition;
        //    m_rootMotion = rootPos - m_lastRootPos;
        //    m_lastRootPos = rootPos;
        //    rootPos.x = 0;
        //    rootPos.z = 0;
        //    m_rootMotion.y = 0;
        //    m_rootBone.localPosition = rootPos;
        //}
        //m_lastAnimTime = (int)animState.normalizedTime;
    }
}